package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author Envy
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;
    /**
     * 加入购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.addShoppingCart(shoppingCart);
    }

    /**
     * 购物车列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> selectshoppingcart() {
        List<ShoppingCart> ssc = shoppingCartService.selectshoppingcart();
        return R.success(ssc);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> cleanshoppingCart() {
        int csc = shoppingCartService.cleanshoppingCart();
        if (csc != 0) {
            return R.success("清空购物车成功");
        }
        return R.error("购物车暂无菜品、套餐，请添加后重试");
    }

    /**
     * 删除购物车商品
     */
    @PostMapping("/sub")
    public R<Object> deleteshoppingCart(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.deleteshoppingCart(shoppingCart);
    }
}
